package cn.always.xiajia.framework.common.util;

import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.hutool.extra.servlet.ServletUtil;

/**
 * 客户端工具类
 * 
 * @author xgj
 *
 */
public class ExtServletUtil extends ServletUtil {

	/**
	 * 获取String参数
	 */
	public static String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	/**
	 * 获取String参数
	 */
	public static String getParameter(String name, String defaultValue) {
		return ExtConvertUtils.toStr(getRequest().getParameter(name), defaultValue);
	}

	/**
	 * 获取Integer参数
	 */
	public static Integer getParameterToInt(String name) {
		return ExtConvertUtils.toInt(getRequest().getParameter(name));
	}

	/**
	 * 获取Integer参数
	 */
	public static Integer getParameterToInt(String name, Integer defaultValue) {
		return ExtConvertUtils.toInt(getRequest().getParameter(name), defaultValue);
	}

	/**
	 * 获取Boolean参数
	 */
	public static Boolean getParameterToBool(String name) {
		return ExtConvertUtils.toBoolean(getRequest().getParameter(name));
	}

	/**
	 * 获取Boolean参数
	 */
	public static Boolean getParameterToBool(String name, Boolean defaultValue) {
		return ExtConvertUtils.toBoolean(getRequest().getParameter(name), defaultValue);
	}

	/**
	 * 获取request
	 */
	public static HttpServletRequest getRequest() {
		return getRequestAttributes().getRequest();
	}

	/**
	 * 获取response
	 */
	public static HttpServletResponse getResponse() {
		return getRequestAttributes().getResponse();
	}

	/**
	 * 获取session
	 */
	public static HttpSession getSession() {
		return getRequest().getSession();
	}

	public static ServletRequestAttributes getRequestAttributes() {
		RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
		return (ServletRequestAttributes) attributes;
	}

	/**
	 * 获取客户端IP
	 * 
	 * @return
	 */
	public static String getClientIP() {
		HttpServletRequest request = getRequest();
		if (request == null) {
			return null;
		}
		return ServletUtil.getClientIP(request);
	}

	/**
	 * 是否以指定字符串开头，忽略大小写
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isJsonRequest(ServletRequest request) {
		return ExtStrUtil.startWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE);
	}

	/**
	 * 前缀是filter_的参数
	 * 
	 * @param request
	 * @return
	 */
	public static Map<String, Object> getParametersStartingWith(ServletRequest request) {
		return getParametersStartingWith(request, "filter_");
	}

	/**
	 * 获取request参数
	 * 
	 * @param request
	 * @param prefix 前缀
	 * @return
	 */
	public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) {
		Enumeration<?> paramNames = request.getParameterNames();
		LinkedHashMap<String, Object> params = new LinkedHashMap<>();
		if (prefix == null) {
			prefix = "";
		}
		while (paramNames != null && paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			if ("".equals(prefix) || paramName.startsWith(prefix)) {
				String unprefixed = paramName.substring(prefix.length());
				String[] values = request.getParameterValues(paramName);
				if (values != null && values.length != 0) {
					if (values.length > 1) {
						params.put(unprefixed, values);
					} else if (!"".equals(values[0].trim())) {
						params.put(unprefixed, values[0].trim());
					}
				}
			}
		}
		return params;
	}

}
